package com.example.utils;

import android.content.Context;
import android.util.Log;

import com.tbruyelle.rxpermissions.RxPermissions;

import rx.functions.Action1;


/**
 * Created by 刘智强 on 2016/9/13.
 */
public class PermissionUtils {
    private PermissionResultCallBack permissionResultCallBack;

    private boolean isGetPermission;
    public PermissionResultCallBack getPermissionResultCallBack() {
        return permissionResultCallBack;
    }
    public void setPermissionResultCallBack(PermissionResultCallBack permissionResultCallBack) {
        this.permissionResultCallBack = permissionResultCallBack;
    }

    /**
     * 单例模式
     *
     * @return
     */
    public static PermissionUtils getInstance() {
        PermissionUtils permissionUtils = null;
        if (permissionUtils == null) {
            permissionUtils = new PermissionUtils();
        }
        return permissionUtils;
    }

    public void requestPermission(Context context, final PermissionResultCallBack permission, final String... permissions) {
        RxPermissions rx = RxPermissions.getInstance(context.getApplicationContext());
//        rx.setLogging(true);
        rx.request(permissions).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean aBoolean) {
                if (aBoolean){
                    permission.onPermissionGranted();
                }else {
                    permission.onPermissionDenied(permissions);
                }

            }
        });
    }


    public interface PermissionResultCallBack {

        /**
         * 当所有权限的申请被用户同意之后,该方法会被调用
         */
        void onPermissionGranted();

        /**
         * 当权限申请中的某一个或多个权限,被用户曾经否定了,并确认了不再提醒时,也就是权限的申请窗口不能再弹出时,
         * 该方法将会被调用
         *
         * @param permissions
         */
        void onPermissionDenied(String... permissions);

        /**
         * 当权限申请中的某一个或多个权限,被用户否定了,但没有确认不再提醒时,也就是权限窗口申请时,但被否定了之后,
         * 该方法将会被调用.
         *
         * @param permissions
         */
        void onRationalShow(String... permissions);
    }
}
